package com.example.dllo.keepdemo.dynamic;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.example.dllo.keepdemo.R;
import com.example.dllo.keepdemo.base.BaseActivity;
import com.example.dllo.keepdemo.dynamic.bean.DynamicDetailsBean;
import com.example.dllo.keepdemo.person.PersonMessageActivity;
import com.example.dllo.keepdemo.tools.CalculateTimeDifference;
import com.example.dllo.keepdemo.tools.GsonRequest;
import com.example.dllo.keepdemo.tools.RoundImageView;
import com.example.dllo.keepdemo.tools.VolleySingleton;

/**
 * Created by dllo on 16/1/16.
 */
public class DynamicDetailActivity extends BaseActivity implements AdapterView.OnItemClickListener {
    private Toolbar toolbar;
    private ListView listView;
    private DynamicDetailsListViewAdapter adapter;
    private DynamicDetailsBean dynamicDetailsBean;
    private TextView tvName,tvContent,tvTime,tvPraise,tvComments;
    private RoundImageView imgHead;
    private ImageView imageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dynamicdetails);
        toolbar = (Toolbar) findViewById(R.id.toolbar_activity_dynamicdetails);
        listView = (ListView) findViewById(R.id.listview_activity_dynamivdetails);
        listView.setDivider(null);
        listView.setOnItemClickListener(this);

        LinearLayout headerLinearLayout = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.header_listview_activity_dynamicdetails,null);
        listView.addHeaderView(headerLinearLayout);
        tvName = (TextView) headerLinearLayout.findViewById(R.id.tv_header_listview_activity_dynamicdetails_name);
        tvContent = (TextView) headerLinearLayout.findViewById(R.id.tv_header_listview_activity_dynamicdetails_content);
        tvTime = (TextView) headerLinearLayout.findViewById(R.id.tv_header_listview_activity_dynamicdetails_time);
        tvPraise = (TextView) headerLinearLayout.findViewById(R.id.tv_header_listview_activity_dynamicdetails_praisenum);
        tvComments = (TextView) headerLinearLayout.findViewById(R.id.tv_header_listview_activity_dynamicdetails_replynum);
        imgHead = (RoundImageView) headerLinearLayout.findViewById(R.id.img_header_listview_activity_dynamicdetails_headpicture);
        imageView = (ImageView) headerLinearLayout.findViewById(R.id.img_header_listview_activity_dynamicdetails_picture);



        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.mipmap.actionlastbtn);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DynamicDetailActivity.this.finish();
            }
        });

        initData();

    }

    private void loadData() {
        tvName.setText(dynamicDetailsBean.getData().getAuthor().getUsername());
        tvContent.setText(dynamicDetailsBean.getData().getContent());
        tvTime.setText(CalculateTimeDifference.timeDifferenceUTC(dynamicDetailsBean.getData().getCreated()));
        tvPraise.setText(dynamicDetailsBean.getData().getLikes() + "");
        tvComments.setText(dynamicDetailsBean.getData().getComments() + "");
        if (dynamicDetailsBean.getData().getAuthor().getAvatar().equals("") || !(dynamicDetailsBean.getData().getAuthor().getAvatar().endsWith(".jpg"))) {
            imgHead.setImageResource(R.mipmap.person_45_45);
        } else {
            ImageLoader.ImageListener listener = ImageLoader.getImageListener(imgHead,R.mipmap.person_45_45,R.mipmap.person_45_45);
            VolleySingleton.getImageLoader().get(dynamicDetailsBean.getData().getAuthor().getAvatar(),listener,80,80);
        }

        if (dynamicDetailsBean.getData().getPhoto().equals("") || !(dynamicDetailsBean.getData().getPhoto().endsWith(".jpg"))) {
            imageView.setVisibility(View.GONE);
        } else {
            ImageLoader.ImageListener listener = ImageLoader.getImageListener(imageView,R.mipmap.placeholder41_41,R.mipmap.placeholder41_41);
            VolleySingleton.getImageLoader().get(dynamicDetailsBean.getData().getPhoto(),listener,390,350);
        }

        imgHead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(DynamicDetailActivity.this,PersonMessageActivity.class);
                intent.putExtra("id",dynamicDetailsBean.getData().getAuthor().get_id());
                startActivity(intent);
            }
        });
    }

    private void initData() {
        Intent intent = getIntent();
        String id = intent.getStringExtra("id");
        String url = "https://api.gotokeep.com/v1.1/entries/" + id + "?limit=20&reverse=true";
        GsonRequest<DynamicDetailsBean> gsonRequest = new GsonRequest<DynamicDetailsBean>(url, DynamicDetailsBean.class, new Response.Listener<DynamicDetailsBean>() {
            @Override
            public void onResponse(DynamicDetailsBean response) {
                dynamicDetailsBean = response;
                adapter = new DynamicDetailsListViewAdapter(DynamicDetailActivity.this,response);
                listView.setAdapter(adapter);
                loadData();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        VolleySingleton.getInstance()._addRequest(gsonRequest);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(this,PersonMessageActivity.class);
        intent.putExtra("id",dynamicDetailsBean.getData().getCommentsList().get(position).getAuthor().get_id());
        startActivity(intent);
    }
}
